/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.biz.crawler.in;

import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.crawler.ContentImageHandler;
import com.yunji.framework_template.common.http.HttpClientUtil;
import com.yunji.framework_template.common.util.ImageUtil;
import com.yunji.framework_template.common.util.StringUtil;
import com.yunji.framework_template.common.web.HttpResult;
import lombok.extern.slf4j.Slf4j;

/**
 * ClassName:TimesOfIndiaNewsContentImageHandler <br/>
 * Date: 2018年12月5日 下午7:51:14 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
@Slf4j
@Service
public class TimesOfIndiaNewsContentImageHandler extends ContentImageHandler {

    @Override
    public String getImage(String contentUrl) {
        try {
            HttpResult result = HttpClientUtil.doGet(contentUrl);
            if (result==null || result.getCode() != 200 || StringUtil.isEmpty(result.getContent())) {
                return null;
            }
            Element contentElement = new Element(Tag.valueOf("contents"), contentUrl);
            contentElement.html(result.getContent());
            Element mainContentElement = contentElement.select("div[class=\"main-content\"]").first();
            Element highLightElement = contentElement.select("section[class=\"highlight clearfix\"]").first();
            Element misskyraPhotoElement = contentElement.select("div[class=\"photogalcont currentphgal\"]").first();
            Element gadgetsnowSlidesElement = contentElement.select("div[class=\"slides\"]").first();
            Element gadgetsnowSlidesElement2 = contentElement.select("div[class=\"img_wrapper col-sm-4\"]").first();
            Element imageSlideElement = contentElement.select("img[class=\"imageSlide\"]").first();
            Element outImageElement = contentElement.select("div[class=\"outer\"]").first();
            Elements imgBlockElements = contentElement.select("div[class=\"imgblock\"]>img");
            Elements figureImageElements = contentElement.select("div[class=\"_3uDoq _3vnjS\"]>figure>img");
            Elements blogImageElements = contentElement.select("div[class=\"article\"]>div[class=\"content\"]>img");
            Element slideContentElement = contentElement.select("div[class=\"slide-content socialshow\"]").first();
            Element imageBoxElement = contentElement.select("div[class=\"imagebox_bg\"]").first();
            Element livheadElement = contentElement.select("div[class=\"livbheading\"]").first();

            Element imgElement = null;

            if (mainContentElement != null) {
                Element hightImage = mainContentElement.select("section[class=\"highlight clearfix\"]").first();
                Element hightVideoImage = mainContentElement.select("section[class=\"highlight clearfix vdo\"]").first();
                Element hightElement = null;
                if (hightImage != null) {
                    hightElement = hightImage;
                } else if (hightVideoImage != null) {
                    hightElement = hightVideoImage;
                }
                if (hightElement != null) {// 从hight中提取图片
                    imgElement = hightElement.select("img").first();
                } else {// 如果没有hight图片，则从内容中提取图片
                    Element articleElement = mainContentElement.select("div[class=\"article_content clearfix\"]").first();
                    if (articleElement != null) {
                        Element imageDivElement = articleElement.select("div[class=\"image\"]").first();
                        if (imageDivElement != null) {
                            imgElement = imageDivElement.select("img").first();
                        }
                    }
                }
            }
            if (imgElement == null && highLightElement != null) {
                imgElement = ImageUtil.getOneImgFromImageElements(highLightElement.select("img"));
            }
            if (imgElement == null && misskyraPhotoElement != null) {
                Elements photoBlockImages = misskyraPhotoElement.select("div[class=\"photo_block\"]");
                if (photoBlockImages != null) {
                    imgElement = ImageUtil.getOneImgFromImageElements(photoBlockImages.select("img"));
                }
            }
            if (imgElement == null && gadgetsnowSlidesElement != null) {
                Element photoBlockImage = gadgetsnowSlidesElement.select("div[class=\"imagebox\"]").first();
                if (photoBlockImage != null) {
                    imgElement = ImageUtil.getOneImgFromImageElements(photoBlockImage.select("img"));
                }
            }
            if (imgElement == null && gadgetsnowSlidesElement2 != null) {
                imgElement = ImageUtil.getOneImgFromImageElements(gadgetsnowSlidesElement2.select("img"));
            }
            if (imgElement == null && imageSlideElement != null) {
                imgElement = imageSlideElement;
            }
            if (imgElement == null && outImageElement != null) {
                imgElement = ImageUtil.getOneImgFromImageElements(outImageElement.select("img"));
            }
            if (imgElement == null && imgBlockElements != null) {
                imgElement = ImageUtil.getOneImgFromImageElements(imgBlockElements);
            }
            if (imgElement == null && figureImageElements != null) {
                imgElement = ImageUtil.getOneImgFromImageElements(figureImageElements);
            }
            if (imgElement == null && blogImageElements != null) {
                imgElement = ImageUtil.getOneImgFromImageElements(blogImageElements);
            }
            if (imgElement == null && slideContentElement != null) {
                imgElement = ImageUtil.getOneImgFromImageElements(slideContentElement.select("img"));
            }
            if (imgElement == null && imageBoxElement != null) {
                imgElement = ImageUtil.getOneImgFromImageElements(imageBoxElement.select("img"));
            }
            if (imgElement == null && livheadElement != null) {
                imgElement = ImageUtil.getOneImgFromImageElements(livheadElement.select("img"));
            }
            if (imgElement == null) {
                String imageUrl = StringUtil.subString(result.getContent(), "\"image\":\"", "\"");
                if (!StringUtil.isEmpty(imageUrl)) {
                    return imageUrl;
                }
                // 支持这种链接中缩略图的提取：https://timesofindia.indiatimes.com/videos/entertainment/hindi/after-fire-mishap-shah-rukh-khan-worried-about-release-of-zero/videoshow/66959131.cms
                imageUrl = StringUtil.subString(result.getContent(), "\"thumbnailUrl\": \"", "\"");
                if (!StringUtil.isEmpty(imageUrl)) {
                    return imageUrl;
                }

            }
            if (imgElement != null) {
                return ImageUtil.getImageFromImageElement(imgElement);
            }
        } catch (Exception e) {
            log.error("Get image from url " + contentUrl + " exception happened:" + e.getMessage(), e);
        }
        return null;
    }

}
